import Vue from "vue";
import Vuex from "vuex";
import router from "../router";
Vue.use(Vuex);

// store.js 或相应的 Vuex 模块
const store = new Vuex.Store({
  state: {
    lastVisitedPath: '/', // 默认首页
    isAuthenticated: false
  },
  mutations: {
    setLastVisitedPath(state, path) {
      state.lastVisitedPath = path
    },
    setAuthenticated(state, isAuthenticated) {
      state.isAuthenticated = isAuthenticated
    }
  },
  getters: {
    isAuthenticated: state => state.isAuthenticated,
    lastVisitedPath: state => state.lastVisitedPath
  },
  actions: {
    login({ commit }, userCredentials) {
      // 登录操作，例如向API发送请求
      //
      // 假设登录成功后
      commit('setAuthenticated', true)
      this.$router.push(this.state.lastVisitedPath) // 跳转到之前的页面
    }
  }
})

export default store
